package com.czk.ssaaliyunrag.controller;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-11-05
 * @Description:
 * @Version: 1.0
 */
@RestController
public class AliyunRagController {

    @Resource(name = "qwenChatClient")
    private ChatClient chatClient;
    @Resource
    private DashScopeApi dashScopeApi;

    /**
     * 调用阿里云上的知识库构建rag服务
     *
     * @param msg
     * @return
     */
    @GetMapping("/aliyunRag/chat")
    public Flux<String> chat(@RequestParam(name = "msg", defaultValue = "A") String msg) {
        DocumentRetriever retriever = new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder()
                        // 知识库名称
                        .withIndexName("AI智能运维")
                        .build());
        return chatClient.prompt().user(msg).advisors(new DocumentRetrievalAdvisor(retriever)).stream().content();

    }


}
